const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    height: app.globalData.height,
    width: app.globalData.width,
    barHeight: app.globalData.barHeight,
    serverUrl: app.globalData.serverUrl,
    input: '',
    search: '',
    tag: 1,
    helpItems: [],
    current_page: 1,
    last_page: 1,
    islastpage: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      search: options.search
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getHelp();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    let current_page = that.data.current_page;
    let last_page = that.data.last_page;
    //不到最后一页，上拉加载下一页
    if (current_page < last_page) {
      that.setData({
        current_page: that.data.current_page + 1,
      });
      that.getTeacher();
    } else {
      //最后一页显示没有更多了
      that.setData({
        islastpage: true,
      });
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  back: function(e) {
    wx.navigateBack({
      delta: 1,
    })
  },

  input: function(e) {
    this.setData({
      input: e.detail.value
    })
  },

  search: function(e) {
    if (this.data.input == '') {

    } else {
      wx.redirectTo({
        url: '/pages/search/search?search=' + this.data.input,
      })
    }
  },
  select: function(e) {
    this.setData({
      tag: e.currentTarget.dataset.tag,
    })
  },
  //获取帮帮订单
  getHelp: function() {
    var that = this;
    try {
      const token = wx.getStorageSync('token');
      if (token) {
        //成功获取token，发送请求
        wx.request({
          url: app.globalData.serverUrl + '/admin/help/gethelp',
          method: 'POST',
          data: {
            token: token,
          },
          success(res) {
            let length = res.data.Help.data.length;
            for (var i = 0; i < length; i++) {
              try {
                res.data.Help.data[i].paths = res.data.Help.data[i].paths.split(',');
              } catch (e) {
                
              }
            };
            that.setData({
              helpItems: res.data.Help.data,
            });
          }
        });
      }
    } catch (e) {
      wx.showToast({
        title: '获取错误，请重新获取！',
        icon: 'success',
        duration: 2000
      });
    }
  },
  //获取棒帮详情
  getDetail: function(e) {
    let item = e.currentTarget.dataset.item;
    item = JSON.stringify(item);

    wx.navigateTo({
      url: '/pages/helpDetail/helpDetail?item=' + item
    })
  },
})